package com.czl.module_main.viewmodel

import androidx.databinding.ObservableField
import com.czl.base.base.BaseBean
import com.czl.base.base.BaseViewModel
import com.czl.base.base.MyApplication
import com.czl.base.binding.command.BindingAction
import com.czl.base.binding.command.BindingCommand
import com.czl.base.config.AppConstants
import com.czl.base.data.DataRepository
import com.czl.base.data.bean.ActivityListBean
import com.czl.base.data.bean.CarItem
import com.czl.base.data.bean.NoticeListBean
import com.czl.base.event.LiveBusCenter
import com.czl.base.event.SingleLiveEvent
import com.czl.base.extension.ApiSubscriberHelper
import com.czl.base.util.RxThreadHelper

class HomeViewModel(application: MyApplication, model: DataRepository) :
    BaseViewModel<DataRepository>(application, model) {

    val uc = UiChangeEvent()

    var areaName = ObservableField(model.getAreaName())
    var showActivityTitle=ObservableField<Boolean>()
    var showNoticeTitle=ObservableField<Boolean>()
    class UiChangeEvent {
        val loadCompleteEvent: SingleLiveEvent<List<CarItem>> = SingleLiveEvent()
        val loadActivityEvent: SingleLiveEvent<ActivityListBean> = SingleLiveEvent()
        val loadNoticeEvent: SingleLiveEvent<NoticeListBean> = SingleLiveEvent()
        val reportEvent: SingleLiveEvent<Void> = SingleLiveEvent()
        val switchAreaEvent: SingleLiveEvent<Void> = SingleLiveEvent()
        val homeRefreshEvent:SingleLiveEvent<Void> = SingleLiveEvent()
    }
    val btnToMoreCLick: BindingCommand<Void> = BindingCommand(BindingAction {
        LiveBusCenter.postToMoreEvent()
    })
    val onRefreshListener: BindingCommand<Void> = BindingCommand(BindingAction {
        getActivityList()
        getNoticeList()
    })
    val onClickReport: BindingCommand<Void> = BindingCommand(BindingAction {
        uc.reportEvent.call()
    })
    val btnParkClick: BindingCommand<Void> = BindingCommand(BindingAction {
//        startContainerActivity(AppConstants.Router.Main.F_HOME_BEFORE)
        showNormalToast("开发中...")
    })

    val onNoticeCommand: BindingCommand<Any> = BindingCommand(BindingAction {
        startContainerActivity(AppConstants.Router.Notice.F_NOTICE)
    })


    val onSwitchAreaCommand: BindingCommand<Any> = BindingCommand(BindingAction {
        uc.switchAreaEvent.call()
    })

    val btnCodingClick: BindingCommand<Void> = BindingCommand(BindingAction {
       showNormalToast("开发中...")
    })
    val btnComplaintClick: BindingCommand<Void> = BindingCommand(BindingAction {
        startContainerActivity(AppConstants.Router.Complaint.F_COMPLAINT_MANAGER)
    })
//    val btnActivityClick: BindingCommand<Void> = BindingCommand(BindingAction {
//        startContainerActivity(AppConstants.Router.Activities.F_ACTIVITIES_LIST)
//    })
//    val btnNoticeClick: BindingCommand<Void> = BindingCommand(BindingAction {
//        startContainerActivity(AppConstants.Router.Activities.F_ACTIVITIES_BULLETIN_LIST)
//    })

    val onNavMyScoreClick: BindingCommand<Void> = BindingCommand(BindingAction {
        startContainerActivity(AppConstants.Router.User.F_MY_SCORE)
    })

    fun getActivityList() {
        model.getActivityList(mapOf(
            "areaNo" to model.getAreaId(),
            "pageNum" to "1",
            "pageSize" to "3",
        ))
            .compose(RxThreadHelper.rxSchedulerHelper(this))
            .subscribe(object : ApiSubscriberHelper<BaseBean<ActivityListBean>>() {
                override fun onResult(t: BaseBean<ActivityListBean>) {
                    if (t.code == 200) {
                        uc.homeRefreshEvent.call()
                        uc.loadActivityEvent.postValue(t.data)
                        if (t.data!!.records.size==0){
                            showActivityTitle.set(false)
                        }else{
                            showActivityTitle.set(true)
                        }
                    }else{
                        showActivityTitle.set(false)
                        uc.homeRefreshEvent.call()
                        showErrorToast(t.msg)
                    }
                }

                override fun onFailed(msg: String?) {
                    showActivityTitle.set(false)
                    uc.homeRefreshEvent.call()
                    showErrorToast(msg)
                }
            })
    }

    fun getNoticeList() {
        model.getNoticeList(mapOf(
            "areaNo" to model.getAreaId(),
            "pageNum" to "1",
            "pageSize" to "3",
        ))
            .compose(RxThreadHelper.rxSchedulerHelper(this))
            .subscribe(object : ApiSubscriberHelper<BaseBean<NoticeListBean>>() {
                override fun onResult(t: BaseBean<NoticeListBean>) {
                    if (t.code == 200) {
                        uc.homeRefreshEvent.call()
                        uc.loadNoticeEvent.postValue(t.data)
                        if (t.data!!.records.size==0){
                            showNoticeTitle.set(false)
                        }else{
                            showNoticeTitle.set(true)
                        }
                    }else{
                        showNoticeTitle.set(false)
                        uc.homeRefreshEvent.call()
                        showErrorToast(t.msg)
                    }
                }

                override fun onFailed(msg: String?) {
                    showNoticeTitle.set(false)
                    uc.homeRefreshEvent.call()
                    showErrorToast(msg)
                }
            })
    }

}